package com.qf.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.order.entity.Workorder;
import com.qf.order.mapper.WorkorderMapper;
import com.qf.order.service.WorkorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project spring_workorder
 * @Package com.qf.order.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/2 22:23
 */
@Service
public class WorkorderServiceImpl implements WorkorderService {

    @Autowired
    private WorkorderMapper workorderMapper;

    @Override
    @Transactional
    public void insert(Workorder workorder) {
        int i = workorderMapper.insertSelective(workorder);
        if (i != 1){
            System.out.println("添加失败");
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        int i = workorderMapper.deleteByPrimaryKey(id);
        if (i != 1){
            System.out.println("删除失败");
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    @Transactional
    public void updateById(Workorder workorder) {
        int i = workorderMapper.updateByPrimaryKeySelective(null);
        if (i != 1){
            System.out.println("修改失败");
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    public Workorder findById(Integer id) {
        return workorderMapper.selectById(id);
    }

    @Override
    public PageInfo<Workorder> findByProjectIdAndPage(Integer pageNum, Integer pageSize, Integer projectId) {

        PageHelper.startPage(pageNum, pageSize);
        List<Workorder> list = workorderMapper.findByProjectId(projectId);
        PageInfo<Workorder> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
